<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 显示消息
 *
 * 显示给前端用户的提示消息
 */
class Message {

    protected $_message = array();

    public function __construct() {
        //默认消息参数
        $this->_message['state'] = 'fail'; //状态 'fail' or 'success'
        $this->_message['message'] = ''; //消息内容
        $this->_message['referer'] = ''; //跳转地址
        $this->_message['refresh'] = false; //是否刷新
        $this->_message['template'] = 'message_admin'; //模板（/application/errors目录下）

        log_message('debug', 'Message Class Initialized');
    }

    /**
     * 使得CI超级全局对象可以直接使用
     *
     * @param    $key
     * @return    mixed
     */
    public function __get($key) {
        $CI = &get_instance();
        return $CI->$key;
    }

    /**
     * 设置消息键值对
     *
     * @param string|array $key 键名
     * @param string $val 键值
     * @return $this
     */
    public function set($key, $val = '') {
        if (is_array($key)) {
            foreach ($key as $_key => $val) {
                $this->set($_key, $val);
            }
        }
        $this->_message[$key] = $val;
        return $this;
    }

    /**
     * 显示消息
     *
     * @param string|array $message
     * @return $this
     */
    public function show($message) {
        $this->output->enable_profiler(false);
        //翻译
        if (is_array($message) && !empty($message)) {
            $message = $this->translation($message[0], $message[1]);
        } else {
            $message = $this->translation($message);
        }
        $this->_message['message'] = $message;
        //重建跳转地址
        if (!empty($this->_message['referer'])) {
            $this->_message['referer'] = site_url($this->_message['referer']);
        }
        ob_start();
        if ($this->input->is_ajax_request()) {
            header("Content-type: application/json");
            $json = json_encode($this->_message);
            echo $json;
        } else {
            if (ob_get_level() > ob_get_level() + 1) ob_end_flush();
            $template = $this->_message['template'];
            unset($this->_message['template']);
            $data = $this->_message; //传递给模板
            include(APPPATH . "errors/{$template}.php");
        }
        $buffer = ob_get_contents();
        ob_end_clean();
        echo $buffer;
        exit;
    }

    /**
     * 翻译
     *
     * @param string $str 翻译内容 (格式如USER:login_error_pwd)
     * @param array $param 参数 (格式如{key}=>'value')
     * @return string 翻译过的内容
     */
    public function translation($str, $param = array()) {
        //如果不存在则试着读取公共的
        if (strpos($str, ':') === false) {
            $this->lang->load('common');
            $_str = $this->lang->line($str);
        } else {
            list($file, $key) = explode(':', $str, 2);
            $file = strtolower($file);
            $this->lang->load($file);
            $_str = $this->lang->line($key);
        }
        //如果没有找到，则返回自身(为了区别是否读取了语言文件, 这里前后加上##)
        if (!$_str) $_str = '##' . $str . '##';
        //参数替换
        foreach ($param as $key => $val) {
            $_str = str_replace($key, $val, $_str);
        }
        return $_str;
    }
}

/* End of file Message.php */
/* Location: ./application/libraries/Message.php */
